-- [[ uncomment it to disable this module ]]--
do return end

g_Tvb = Proto("testTvb", "testTvb Descriptor")

print("Script testTvb")

-- print('ftypes')
-- for k,t in pairs(ftypes) do
    -- print(k,t)
-- end

-- print('base')
-- for k,t in pairs(base) do
    -- print(k,t)
-- end

-- define protocol fields


-- define dissector function (Tvb, Pinfo, TreeItem)
function g_Tvb.dissector(buf, pkt, tree)
    print('which one called me?', pkt.proto_name)
    len=0;
    local pktlen = buf:reported_length_remaining()
    
    print(type(buf), buf)           -- __tostring
    
    print(buf:reported_len(), buf:reported_length_remaining(), buf:captured_len(), buf:len(), buf:offset())

    print("+++ test[.]  tvb:__call(off, len)")
    print('buf(0, 2):', buf(0, 2))                -- __call
    print('buf(-4, 4):', buf(-4, 4))
    -- print(buf(-4, 8))               -- error: out of bounds 
    print('buf(0, pktlen):', buf(0, pktlen))
    print("--- test[.]  ok")
    print("")
    
    print("+++ test[0]  tvb:range(off, len)")
    print('buf:range(0, 2):', buf:range(0, 2))          -- range
    print('buf:range(-4, 4):', buf:range(-4, 4))
    print('buf:range(-5, 1):', buf:range(-5, 1))
    -- print(buf:range(-4, 8))         -- error: out of bounds 
    print("--- test[0]  ok")
    print("")

    print("+++ test[1]  tvb:bytes(off, len)")
    print('buf:bytes(0, 4):', buf:bytes(0, 4), 'type(buf:bytes(0, 4)):', type(buf:bytes(0, 4)))              -- bytes
    -- print(buf:bytes(-4, 8))              -- error
    print("--- test[1]  ok")
    print("")

    print("+++ test[2]  tvb:raw(off, len)")
    print('buf:raw(0, 4):', buf:raw(0, 4), 'type(buf:raw(0, 4)):', type(buf:raw(0, 4)))              -- raw
    print('buf:raw(-4, 4):', buf:raw(-4, 4))              -- raw
    -- print(buf:raw(-4, 8))              -- error
    print("--- test[2]  ok")
    print("")

    local range = buf(1, pktlen-1)
    print('type(range):', type(range), 'range:', range, 'range:tvb():', range:tvb())
    print(range:len(), range:offset())

    print("+++ test[3]  tvbrange:bytes(encode)")
    bytes, l = range:bytes(0)
    print('bytes, l:', bytes, l)
    print(ENC_STR_HEX | ENC_UTF_8)
    bytes, l = range:bytes(ENC_STR_HEX | ENC_ASCII | ENC_SEP_NONE)
    print('bytes, l:', bytes, l)
    print("--- test[3]  ok")
    print("")

    print("+++ test[4]  tvbrange:range(off, len)")
    print('range:range(0, 4):', range:range(0, 4), 'type(range:range(0, 4)):', type(range:range(0, 4)))              -- bytes
    print('range:range(0, -1):', range:range(0, -1))              -- range
    print('range:range(-4, 4):', range:range(-4, 4))              -- range
    -- print(range:range(-4, 8))              -- error
    print("--- test[4]  ok")
    print("")

    print("+++ test[5]  tvbrange:raw(off, len)")
    print('range:raw(0, 4):', range:raw(0, 4), 'type(range:raw(0, 4)):', type(range:raw(0, 4)))              -- raw
    print('range:raw(0, -1):', range:raw(0, -1))              -- raw
    print('range:raw(-4, 4):', range:raw(-4, 4))              -- raw
    -- print(range:raw(-4, 8))              -- error
    print("--- test[5]  ok")
    print("")

    range = buf(-4, 4)
    print("+++ test[6]  tvbrange:bitfield(off, len)")
    print('range:', range)
    print('range:bitfield(0, 32):', range:bitfield(0, 32))              -- bitfield
    print("--- test[6]  ok")
    print("")

    print("Script testTvb dissector exit")
end

print("Script testTvb exit")
